//
//  PersonalViewController.m
//  Medgretraining
//
//  Created by 辰领科技 on 16/5/17.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "PersonalViewController.h"
#import "PersonInfoViewController.h"
#import "TrainingCertificationViewController.h"
#import "walletViewController.h"

@interface PersonalViewController ()
@property (strong, nonatomic) IBOutlet UIView *navView;
@property (strong, nonatomic) IBOutlet UIScrollView *scroView;
@property (weak, nonatomic) IBOutlet UIImageView *personHead;//头像
@property (strong, nonatomic) IBOutlet UIButton *headerButton;//头像按钮
@property (weak, nonatomic) IBOutlet UILabel *personName;//昵称
@property (strong, nonatomic) IBOutlet UIButton *LoginButton;
@property (strong, nonatomic) IBOutlet UIButton *museTypeButton;//实名认证 && 推广员端

@property(nonatomic,strong)NSString * headImage;
@property(nonatomic,strong)NSDictionary *dic;

@property(nonatomic,strong)NSDictionary *queryDataArray;

@property (strong, nonatomic) IBOutlet UIView *ViewOne;
@property (strong, nonatomic) IBOutlet UIView *ViewTwo;
@property (strong, nonatomic) IBOutlet UIView *ViewThree;

@end

@implementation PersonalViewController



- (void)viewWillAppear:(BOOL)animated{
    [[NSUserDefaults standardUserDefaults]setInteger:4 forKey:@"selectedIndex"];
    if([Common getLoginStatus]){
         [_LoginButton setTitle:@"退出登录" forState:UIControlStateNormal];
         [self getPersonalInfo];
         [self queryData];
    }else{
         [_personHead setImage:[UIImage imageNamed:@"my8"]];
         _personName.text = @"您还没有登陆哦";
         [_LoginButton setTitle:@"马上登录" forState:UIControlStateNormal];
    }
}

#pragma mark - 登陆 && 退出登录
- (IBAction)Login:(id)sender {
     if ([Common getLoginStatus]) {
          NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
          [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
          self.shareApi.delegate=self;
          [self.shareApi  requestOperationWithDic:dic withPath:JY_exitLogin withHttpType:@"POST" withTag:JY_JY_exitLogin];

     } else {
          [[AppDelegate sharedAppDelegate]ShowLoginPage];
          return;
     }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(hadLogin) name:@"hadLogin" object:nil];
    self.dic = [NSDictionary dictionary];
    [self initViewLayout];
}

- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [self adaptiveViewLayout:self.scroView];
    [self adaptiveViewLayout:self.ViewOne];
    [self adaptiveViewLayout:self.ViewTwo];
    [self adaptiveViewLayout:self.ViewThree];

     _scroView.frame = CGRectMake(0, 64*VerticalRatio(), 320*VerticalRatio(), 504*VerticalRatio()-49);
     _scroView.contentSize = CGSizeMake(0, 504*VerticalRatio()-48);
    
    _personHead.layer.masksToBounds = YES;
    _personHead.layer.cornerRadius = 27.5*VerticalRatio();
    _LoginButton.layer.cornerRadius = 5*VerticalRatio();
    _LoginButton.layer.borderColor = [UIColor colorWithRed:0.208 green:0.396 blue:0.592 alpha:1.000].CGColor;
    _LoginButton.layer.borderWidth = 1;
    
    
}

- (void)hadLogin{
    self.tabBarController.selectedIndex = 0;
}


#pragma mark  ==== 查询个人资料
- (void)getPersonalInfo{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][@"userId"] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:JY_queryMemberInfoById withHttpType:@"POST" withTag:JY_JY_queryMemberInfoById];
}

#pragma mark ==== 查看会员实名认证
- (void)queryData {
     NSMutableDictionary * dic = [NSMutableDictionary dictionary];
     [dic setObject:[Common getLoginInfo][Key_User_userId] forKey:@"museId"];
     [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
     [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];
     
     self.shareApi.delegate = self;
     [self.shareApi  requestOperationWithDic:dic withPath:JY_memberUserCertification withHttpType:@"POST" withTag:JY_JY_memberUserCertification];
}


#pragma mark - 头像点击事件
- (IBAction)personInfo:(id)sender {
     if (![Common getLoginStatus]) {
          [[AppDelegate sharedAppDelegate]ShowLoginPage];
          return;
     }
    UIStoryboard *secondStroyBoard=[UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    PersonInfoViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"PersonInfo"];
    page.state = [NSString stringWithFormat:@"%@",self.dic[@"muceStatus"]];
        page.attestDic = _dic;
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 更多
- (IBAction)more:(id)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"More"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 我的订单
- (IBAction)problem:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"MyOrder"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 我的评论
- (IBAction)video:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"Comments"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 我的收藏
- (IBAction)evaluation:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"collection"];
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 我的钱包
- (IBAction)collection:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }

    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
     walletViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"wallet"];
     NSString *museType = [NSString stringWithFormat:@"%@",self.dic[@"museType"]];
     
     if ([museType isEqualToString:@"1"]) {
          page.isWithdrawal = NO;
     } else if ([museType isEqualToString:@"4"]) {
          page.isWithdrawal = YES;
     }
     
    
    [[Common getNavigation]pushViewController:page];
}

#pragma mark - 家教认证 && 推广人员端
- (IBAction)certification:(id)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    
    NSString *museType = [NSString stringWithFormat:@"%@",self.dic[@"museType"]];
    if ([museType isEqualToString:@"4"]) {
         
         UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"TeachStoryboard" bundle:nil];
         NSString *museIsCert = [NSString stringWithFormat:@"%@",_dic[@"museIsCert"]];
         if ([museIsCert isEqualToString:@"0"]) {
              //未提交实名认证
              TrainingCertificationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"TrainingCertification"];
              page.muceStatus = @"0";
              [[Common getNavigation]pushViewController:page];
         } else if ([museIsCert isEqualToString:@"1"]) {
              //已提交实名认证
              NSString *muceStatus = [NSString stringWithFormat:@"%@",_queryDataArray[@"muceStatus"]];
              if ([muceStatus isEqualToString:@"1"]) {
                   //审核中
                   UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"InTheAuthentication"];
                   [[Common getNavigation]pushViewController:page];
              } else if ([muceStatus isEqualToString:@"2"]) {
                   //审核通过
                   TrainingCertificationViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"TrainingCertification"];
                   page.dic = self.queryDataArray;
                   page.muceStatus = @"1";
                   [[Common getNavigation]pushViewController:page];
              } else {
                   //审核不通过
                   TrainingCertificationViewController *page = [secondStroyBoard     instantiateViewControllerWithIdentifier:@"TrainingCertification"];
                   page.dic = self.queryDataArray;
                   page.muceStatus = @"2";
                   [[Common getNavigation]pushViewController:page];
              }
         }
         
    } else if ([museType isEqualToString:@"3"]) {
        UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
        UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"promote"];
        [[Common getNavigation]pushViewController:page];
    }
}

#pragma mark - 修改密码
- (IBAction)ChangeThePassword:(UIButton *)sender {
    if (![Common getLoginStatus]) {
        [[AppDelegate sharedAppDelegate]ShowLoginPage];
        return ;
    }
    UIStoryboard *secondStroyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    UIViewController *page = [secondStroyBoard instantiateViewControllerWithIdentifier:@"changePassword"];
    [[Common getNavigation]pushViewController:page];
}



#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    if (tag==JY_JY_queryMemberInfoById) {
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else {
            _dic = database[@"result"];
            
            NSString *museType = [NSString stringWithFormat:@"%@",self.dic[@"museType"]];
            if ([museType isEqualToString:@"1"]) {
//                [self.museTypeButton setTitle:@"   家教认证" forState:UIControlStateNormal];
//                [self.museTypeButton setImage:[UIImage imageNamed:@"my5.png"] forState:UIControlStateNormal];
                 self.ViewOne.frame = CGRectMake(0, 178*VerticalRatio(), 320*VerticalRatio(), 189*VerticalRatio());
                 self.ViewTwo.frame = CGRectMake(0, 90*VerticalRatio(), 320*VerticalRatio(), 100*VerticalRatio());
                 self.ViewThree.frame = CGRectMake(0, 0, 320*VerticalRatio(), 54*VerticalRatio());
            } else if ([museType isEqualToString:@"3"]) {
                [self.museTypeButton setTitle:@"   推广员端" forState:UIControlStateNormal];
                [self.museTypeButton setImage:[UIImage imageNamed:@"my6.png"] forState:UIControlStateNormal];
                 self.ViewOne.frame = CGRectMake(0, 132*VerticalRatio(), 320*VerticalRatio(), 189*VerticalRatio());
                 self.ViewTwo.frame = CGRectMake(0, 46*VerticalRatio(), 320*VerticalRatio(), 100*VerticalRatio());
                 self.ViewThree.frame = CGRectMake(0, 44*VerticalRatio(), 320*VerticalRatio(), 54*VerticalRatio());
            } else if ([museType isEqualToString:@"4"]) {
                 [self.museTypeButton setTitle:@"   家教认证" forState:UIControlStateNormal];
                 [self.museTypeButton setImage:[UIImage imageNamed:@"my5.png"] forState:UIControlStateNormal];
                 self.ViewOne.frame = CGRectMake(0, 178*VerticalRatio(), 320*VerticalRatio(), 189*VerticalRatio());
                 self.ViewTwo.frame = CGRectMake(0, 91*VerticalRatio(), 320*VerticalRatio(), 100*VerticalRatio());
                 self.ViewThree.frame = CGRectMake(0, 44, 320*VerticalRatio(), 54*VerticalRatio());

            }
            
            NSString * image = [NSString stringWithFormat:@"%@",_dic[@"museImage"]];
            _headImage = image;
            if (image && image.length>0 && ![_dic[@"museImage"] isEqual:[NSNull null]]) {
                [_personHead sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IP_Address_ImageDown_Fresh,image]]];
                
            }
            
            if ([_dic[@"museNickName"] isEqual:[NSNull null]]) {
                _personName.text = @"";
            }else{
                _personName.text = [NSString stringWithFormat:@"%@",_dic[@"museNickName"]];
            }
        }
    } else if (tag == JY_JY_exitLogin) {
         if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
              
              [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
              
         }else {
              [[CWHUDTips sharedInstance]  showTips:@"退出登录成功"  withStatus:2 dismissAfter:1.5];
              [Common DeleteSingleFile];
              [[AppDelegate sharedAppDelegate]  ShowLoginPage];
              [_personHead setImage:[UIImage imageNamed:@"my8"]];
              _personName.text = @"您还没有登陆哦";
              [_LoginButton setTitle:@"马上登录" forState:UIControlStateNormal];
         }

    } else if (tag == JY_JY_memberUserCertification){
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
             self.queryDataArray = database[@"result"];
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
        
        [self dismissWaitDialog];
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"hadLogin" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

    

@end
